<?php
/**
 * 获取每日考勤记录详情bll
 */
apf_require_class("Bll_BLL");
class Bll_Attendance_LeaveRecord extends BLL{

    /**
     * 根据用户ID获取所有请假记录,用于我的考勤，检查是否在请假状态
     *
     * @param int $user_id
     * @access public
     * @return empty or int
     * @author coldarmy
     */
    public function get_leave_data($user_id) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $where = array('user_id' => $user_id,'is_cancel'=>0);
        $tmp = $this->get_app_dao()->find($where,'id desc',0,0);
        foreach ($tmp as $k => $v) {
            $result[$k]['start'] = strtotime($v['start_time']);
            $result[$k]['end'] = strtotime($v['end_time']);
            $result[$k]['type'] = $v['type'];
            $result[$k]['id'] = $v['id'];
            $result[$k]['apply_time_ext'] = $v['apply_time_ext'];
            $result[$k]['leave_reason'] = $v['leave_reason'];
            $result[$k]['status'] = $v['status'];
            $result[$k]['leave_desc'] = $v['leave_desc'];
            $result[$k]['img'] = $v['img'];
        }
        return $result;
    }

    /**
     * 根据用户id ,请假id更新数据
     *
     * @param mixed $id
     * @param mixed $user_id
     * @param mixed $data
     * @access public
     * @return void
     */
    public function update($id,$user_id,$data) {
        if(empty($id) || empty($user_id) || empty($data)){
            return '';
        }
        $where['user_id'] = $user_id;
        $where['id'] = $id;
        return $this->get_app_dao()->update($data,$where);
    }

    /**
     * 根据ID获取申请事由
     * @param int $id
     * @access public
     * @return empty or int
     * @author Jerry
     */
    public function get_leave_by_id($id) {
        $result = array();
        if(empty($id)) {
            return $result;
        }
        $result = $this->get_app_dao()->find_by_id(intval($id));
        return $result ? $result : array();
    }
    public function get_leave_by_ids($ids){
        $result = array();
        if(empty($ids)) {
            return $result;
        }
        return $this->get_app_dao()->find_by_ids($ids);
    }

    /**
     * 根据条件获取请假记录
     *
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param int $page
     * @access public
     * @author coldarmy
     * @return empty or int
     */
    public function get_leave_record_data($where,$limit = 7,$page = 1,$order = 'id desc') {
        $result = array();
        $offset = $page > 1?$limit*($page - 1):0;
        $result = $this->get_app_dao()->find($where,$order,$limit,$offset);
        return $result;
    }

    /**
     * 根据请假开始，结算时间获取记录
     * @param array $uids
     * @param string $start start_time >= $start
     * @param string $end   end_time <= $end
     * @return array
     * **/
    public function get_leave_record_by_between($uids,$start,$end) {
        $result = array();
        $result = $this->get_app_dao()->get_leave_record_by_between($uids,$start,$end);
        return $result;
    }
    /**
     * 指定一个月份获取时间跨度的请假，查询201204 申请时间为0429 0:00 -0430 23:59 AND 0501:00-0528 23:59
     * @param int $uids
     * @param int $year 2012
     * @param int $month 5
     */
    public function get_leave_record_use_apply_time($uid,$year,$month){

        $start_s = date('Y-m-d H:i:s',mktime(0,0,0,$month-1,1,$year));
        $start_e = date('Y-m-d',mktime(0,0,0,$month+1,0,$year)).' 23:59:59';

        //调整跨度区域 申请时间大于上个月结算日，start 从上个月1好开始，end 到上个月最后一天......这里直接取两个月请假记录
        $where['user_id'] = $uid;
        $where['status'] = '已批准';
        $where['start_time >= ?'] = $start_s;
        $where['end_time <= ?'] = $start_e;
        $result = $this->get_app_dao()->find($where);
        $lids = array();
        if(!empty($result)){//去重
            foreach($result as $k=>$val){
                if(in_array($val['id'],$lids)){
                    unset($result[$k]);
                }else{
                    $lids[]=$val['id'];
                }
            }
        }
        return $result;

    }
    /**
     * 根据指定条件获取总记录数
     *
     * @param mixed $where
     * @access public
     * @author coldarmy
     * @return int
     */
    public function get_count_by_condition($where){
        $count = 0;
        $count = $this->get_app_dao()->find_count($where);
        return $count;
    }

    /**
     * 根据用户id,及输入的时间段获取年假
     *
     * @param mixed $userid
     * @param int $start
     * @param int $end
     * @access public
     * @return void
     */
    public function get_annual_by_condition($userid,$start = 0,$end =0){
        $result = 0;
        if(empty($userid)) {
            return $result;
        }
        $result = $this->get_app_dao()->get_annual_between($userid,$start,$end);
        return $result[0]['leave_time'];
    }

    /**
     * @return Dao_Attendance_LeaveRecord
     */
    private function get_app_dao(){
        return $this->get_dao('Dao_Attendance_LeaveRecord');
    }

    /**
     * 检测这次请的假中间是否有某一天已请过或正在审批
     * @param string $start 2013-01-01 09:00
     * @param string $end 2013-01-01 09:00
     * @param string $type
     * @param int $user_id
     */
    public function is_check_same_leave($start,$end,$type,$uid){
        $result = array();
        if($type=='公出'){
            return $result;
        }
        apf_require_class('Bll_Attendance_DailyReport');
        apf_require_class('Bll_Attendance_DailyLeave');
        $bll = new Bll_Attendance_DailyReport();

        $daily_bll = new Bll_Attendance_DailyLeave();

        $leave_data['start_time'] = strtotime($start);
        $leave_data['end_time'] = strtotime($end);
        $leave_data['type'] = $bll->format_leave_type($type);
        $leave_data['apply_time_ext']= time();
        $start_m = date('Ymd',$leave_data['start_time']);
        $start_d = date('Ymd',$leave_data['end_time']);
        //user_last_leave_record user_leave_daily,user_leave_record_ext,取数据
        $result = $bll->split_leave_days($leave_data);
        $has_days = array();
        $where=array(
                'user_id'=>$uid,
                "dateline >=$start_m AND dateline <=$start_d AND 1=?"=>1
        );
        if(!empty($result['curr'])){
            $curr_data = $daily_bll->get_curr_data($where);
            if(!empty($curr_data)){
                foreach($curr_data as $val){
                    if(in_array($val['leave_type'],array(1,2,3)) || !in_array($val['leave_status'],array(1,2))){//不算迟到，zao退 公出
                        continue;
                    }
                    $day = $val['dateline'];
                    if(!empty($result['curr'][$day])){
                        $ask_leave = $result['curr'][$day];
                        $ck_a = $this->_format_apm($val['leave_apm']);
                        $ck_b = $this->_format_apm($ask_leave['apm']);
                        if(array_intersect ($ck_a, $ck_b)){//有交集说明有问题
                            $has_days[]= $day;
                        }
                    }
                }
            }
        }

        if(!empty($result['prev'])){
            //得到表user_last_leave_record
            $prev_data = $daily_bll->get_prev_data($where);
            if(!empty($prev_data)){
                foreach($prev_data as $val){
                    if(!in_array($val['leave_status'],array(1,2))){//
                        continue;
                    }
                    $day = $val['dateline'];
                    if(!empty($result['prev'][$day])){
                        $ask_leave = $result['prev'][$day];
                        $ck_a = $this->_format_apm($val['leave_apm']);
                        $ck_b = $this->_format_apm($ask_leave['apm']);
                        if(array_intersect ($ck_a, $ck_b)){//有交集说明有问题
                            $has_days[]= $day;
                        }
                    }
                }
            }
        }
        if(!empty($result['next'])){
            $next_data = $daily_bll->get_next_data($where);
            if(!empty($next_data)){
                foreach($next_data as $val){
                    $day = $val['dateline'];
                    if(!empty($result['next'][$day])){
                        $ask_leave = $result['next'][$day];

                        $ck_b = $this->_format_apm($ask_leave['apm']);
                        if(in_array($val['am_leave_status'],array(1,2))){
                            $ck_a[] = 1;
                        }
                        if(in_array($val['pm_leave_status'],array(1,2))){
                            $ck_a[] = 2;
                        }
                        if(array_intersect ($ck_a, $ck_b)){//有交集说明有问题
                            $has_days[]= $day;
                        }
                    }
                }
            }
        }
        if(!empty($has_days)){
            $has_days = array_unique($has_days);
        }
        return $has_days;
    }
    private function _format_apm($apm){
        $res = array();
        if($apm==3){
            $res=array(1,2);
        }else{
            $res=array($apm);
        }
        return $res;
    }
    public function is_ever_apply($start,$end,$type,$user_id){
        $result=array(
                'is_ever' => 0,
                'start_time'=>'',
                'end_time'=>'',
                'type' =>''
        );
        if($type=='公出'){
            return $result;
        }
        $where = array(
                'user_id'=>$user_id,
                'start_time'=>$start,
                'end_time'=>$end,
                'type'=>$type,
                "status IN('已批准','未审核') AND 1=?"=>1
                );
        $res = $this->get_app_dao()->find($where);
        if(!empty($res)){
            $result=array(
                    'is_ever' => 1,
                    'start_time'=>$start,
                    'end_time'=>$end,
                    'type' =>$type
            );
        }
        return $result;
    }

}
